package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 添加菜品
     * @param dishDTO
     */
    @Override
    public void addDishWithFlavor(DishDTO dishDTO) {
        //保存菜品信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.addDish(dish);
        //保存口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }

            dishFlavorMapper.addFlavors(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult dishPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO,dish);


        List<DishVO> list = dishMapper.DishPage(dish);
        Page<DishVO> p = (Page<DishVO>) list;
        return new PageResult(p.getTotal(),p.getResult());
    }

    /**
     * 批量删除菜品
     * @param ids
     */

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void deleteDish(String ids) {
        //删除菜品
        String[] split = ids.split(",");
        List<Long> list = new ArrayList<>();
        for (String s : split) {
            list.add((long) Integer.parseInt(s));
        }
        //判断菜品是否在售
        for (Long id : list) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == 1){
                throw new DeletionNotAllowedException("当前菜品正在售出，不能删除");
            }
        }


//        List<Dish>  dishList = dishMapper.getById(list);



        //判断菜品是否在套餐当中
//        int count = dishMapper.countInSetmealAndDishByDishId(list);
//        if (count > 0){
//            //当前菜品在套餐当中，不能删除
//            throw new DeletionNotAllowedException("当前菜品在套餐当中，不能删除");
//        }
        //上面的方法复用性不高,我们之后还得写根据菜品id查询的接口,所以用下面的方法
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(list);
        if (setmealIds != null && setmealIds.size() > 0){
            throw new DeletionNotAllowedException("当前菜品在套餐当中，不能删除");
        }



        dishMapper.deleteDish(list);
        //删除口味
        dishFlavorMapper.deleteByDishId(list);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public DishVO getById(Long id) {
        //根据id查询菜品信息并封装到DishVO中
        Dish dish = dishMapper.getById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        dishVO.setFlavors(flavors);

        return dishVO;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);

        List<Long> ids = new ArrayList<>();
        ids.add(dish.getId());
        dishFlavorMapper.deleteByDishId(ids);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0){
            Long dishId = dish.getId();
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            dishFlavorMapper.addFlavors(flavors);
        }


    }

    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        List<Dish> list = dishMapper.findByCategoryId(categoryId);
        return list;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<DishVO> dishVOList = dishMapper.DishPage(dish);

        for (DishVO d : dishVOList) {
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());
            d.setFlavors(flavors);
        }
        return dishVOList;
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
                dishMapper.updateDish(dish);
    }
}
